﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using UniMana.Data;
namespace UniMana.Data
{
    public static class Graduate_ExamData
    {
        public static IQueryable GetList(this Table<Graduate_Exam> tExam, Table<Catalogue_Level> tLevel,
                                        Table<Catalogue_Major> tMajor,Table<Catalogue_Course> tCourse,
                                        bool _bLock = true, int _iDeparmentKey = 0,
                                        int _iLevelKey =0, int _iMajorKey = 0)
        {
            var l = from exam in tExam
                    from level in tLevel
                    from major in tMajor
                    from course in tCourse

                    where exam.Catalogue_LevelKey == level.Catalogue_LevelKey
                    where exam.Catalogue_MajorKey == major.Catalogue_MajorKey
                    where exam.Catalogue_CourseKey == course.Catalogue_CourseKey

                    select new
                    {
                        exam.Graduate_ExamKey,
                        exam.ExamID,
                        exam.Lock,
                        exam.DateExam,
                        level.Catalogue_LevelKey,
                        Level = level.Name,
                        major.Catalogue_MajorKey,
                        major.Catalogue_DepartmentKey,
                        Major = major.Name,
                        course.Catalogue_CourseKey,
                        Course = course.Name

                    };
            if (_bLock == false)
            {
                l = l.Where(p => p.Lock == false || p.Lock == null);
            }
            if (_iDeparmentKey != 0)
            {
                l = l.Where(p => p.Catalogue_DepartmentKey == _iDeparmentKey);
            }
            if (_iLevelKey != 0)
            {
                l = l.Where(p => p.Catalogue_LevelKey == _iLevelKey);
            }
            if (_iMajorKey != 0)
            {
                l = l.Where(p => p.Catalogue_MajorKey == _iMajorKey);
            }
            //foreach (var ll in l)
            //{
            //    DateTime.Parse(ll.DateExam.ToString()).ToShortDateString();
            //}
            return l;
        }

        public static IQueryable GetListExam(this Table<Graduate_Exam> tExam, Table<Catalogue_Level> tLevel,
                                        Table<Catalogue_Major> tMajor, Table<Catalogue_Department> tDepartment,
                                        bool _bLock = true)
        {
            var l = from exam in tExam
                    from level in tLevel
                    from major in tMajor
                    from department in tDepartment 

                    where exam.Catalogue_LevelKey == level.Catalogue_LevelKey
                    where exam.Catalogue_MajorKey == major.Catalogue_MajorKey
                    where major.Catalogue_DepartmentKey == department.Catalogue_DepartmentKey

                    select new
                    {
                        exam.Graduate_ExamKey,
                        exam.ExamID,
                        Lock = exam.Lock == null? false:true,
                        exam.DateExam,
                        level.Catalogue_LevelKey,
                        LevelName = level.Name,
                        major.Catalogue_MajorKey,
                        major.Catalogue_DepartmentKey,
                        MajorName = major.Name,
                        DepartmentName = department.Name
                    };
            if (_bLock == false)
            {
                l = l.Where(p => p.Lock == false || p.Lock == null);
            }
            return l;
        }
    }
}
